/* 
 * File:   config.h
 * Author: bram
 *
 * Created on December 6, 2013, 6:01 PM
 */

#ifndef CONFIG_H
#define	CONFIG_H

#ifdef	__cplusplus
extern "C" {
#endif

#include "types.h"

struct pidValueReference_t
{
    const char * name;
    unsigned char isPIDreference;
    const q_t * pointer;
};

struct PidConfig_t {
	BYTE modeActive;


    q_t setpointTranslate;
    q_t setpointScale;
    q_t setpointClip[2];

    q_t feedbackTranslate;
    q_t feedbackScale;

	q_t motorMix[8];

	q_t P, I, D;
	q_t I_limit;
	q_t outputClip[2];

    BYTE setpointRef;
    BYTE feedbackRef;

    const struct pidValueReference_t * setpoint;
    const struct pidValueReference_t * feedback;
};

struct FlightModeSetup_t
{
    BYTE channel1;
    SHORT range1[2];
    
    BYTE channel2;
    SHORT range2[2];
};

struct Config_t
{
    struct PidConfig_t pid[16];

	struct
	{
		unsigned short freq;
		BYTE filter;
        BYTE useSemaphore;
        
		q_t quaternionBeta;	// = 0.1f;
        q_t complementaryAccWeight;

        char quatAccSetup[3]; // -3 to 3
        char quatGyroSetup[3]; // -3 to 3
        char quatMagnSetup[3]; // -3 to 3
	} imu;

	struct
	{
        BYTE motors;
        BYTE motorMapping[8];
        BYTE enableMotorOutput;

        q_t vLipoScale;
        q_t currentScale;
        SHORT currentOffset;

        q_t vLipoWarning;
        q_t vLipoCritical;

        struct FlightModeSetup_t flightmode[4];
	} base;

	struct
	{
        unsigned short  hardwareFreq;

		BYTE			hardwareLPF;
        BYTE            hardwareGyroRange;
        BYTE            hardwareAccRange;

		q_vector_t	accOffset;
		q_vector_t	accScale;

		BYTE			accSoftwareAvg;
        BYTE			gyroSoftwareAvg[2];

        q_vector_t	accCalibration;		// gravity vector - average of X samples
	} mpu6050;

	struct 
	{
		BYTE            hardwareFreq;
        BYTE			hardwareAvg;
		BYTE			hardwareGain;

		BYTE			softwareAvg;
	} hmc5883;

	struct 
	{
        unsigned short  hardwareFreq;
        BYTE            hardwareMode;
        BYTE            hardwareOversampling;
        
		BYTE			softwareAvg;

	} mpl3115;
};

extern struct Config_t _config;
//#define config() (&_config
//static inline struct ConfigStruct * config() { return &_config; }
#define CONFIG (_config)

void configDefault();

#ifdef	__cplusplus
}
#endif

#endif	/* CONFIG_H */

